<?php

namespace CHOMP\SGIBundle\Controller;

use CHOMP\SGIBundle\Form\PermisoChompType;
use CHOMP\SGIBundle\Form\RolType;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use CHOMP\SGIBundle\Entity\Empresa;
use CHOMP\SGIBundle\Entity\PermisoAutorizacion;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\CallbackValidator;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormError;
use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use CHOMP\SGIBundle\PermisoChomp\PermisoChomp;
use CHOMP\SGIBundle\PermisoChomp\RolChomp;
use CHOMP\SGIBundle\Services\SessionManager;
use JMS\DiExtraBundle\Annotation as DI;
use Symfony\Component\HttpFoundation\Session\Session;


class SeguridadController extends Controller
{
	/**
	 * 
	 * @var SessionManager
	 * @DI\Inject("session.manager")
	 */
	public $sessionSvc;
		
	public function indexAction(Request $request){ 
		if ($this->sessionSvc->userIsLogged()){
			$session = $request->getSession();
			
			$em = $this->getDoctrine()->getManager();
			$query = $em->createQueryBuilder()->select('u')->from('CHOMPSGIBundle:Rol','u')->where('u.empresa = '.$session->get('empresa'));
			
			$paginador = new Pagerfanta(new DoctrineORMAdapter($query));
			$paginador->setMaxPerPage(100);
		
			return $this->render('CHOMPSGIBundle:Seguridad:listar.html.twig', array('roles' => $paginador)); 
		}else{
			return $this->redirect($this->generateUrl('chompsgi_homepage'));	
		}
	}	

	public function claveAction(Request $request){ 
		if ($this->sessionSvc->userIsLogged()){
			$usuario = $this->sessionSvc->getCurrentUser();		
			return $this->render('CHOMPSGIBundle:Seguridad:claveNueva.html.twig', array('empresa' => $usuario->getEmpresa())); 
		}else{
			return $this->redirect($this->generateUrl('chompsgi_homepage'));	
		}
	}	

	public function claveNuevaAction(Request $request){ 
		if ($this->sessionSvc->userIsLogged()){

			$empresa = $this->sessionSvc->getCurrentUser()->getEmpresa();		
			$strClave = '';
			$regClave = '';
			
			$letraMinChk = $request->get('letraMinChk');
			$letraMayChk = $request->get('letraMayChk');
			$numeroChk = $request->get('numeroChk');
			$caracterEspChk = $request->get('caracterEspChk');
			$blancoChk = $request->get('blancoChk');
			
			if(!is_null($letraMinChk)){
				if($strClave == ''){
					$strClave = $request->get('letraMin');
				}else{
					$strClave = $strClave.', '. $request->get('letraMin');
				}
				if($regClave == ''){
					$regClave = $request->get('letraMinExp');
				}else{
					$regClave = $regClave.', '. $request->get('letraMinExp');
				}
			}			
			if(!is_null($letraMayChk)){
				if($strClave == ''){
					$strClave = $request->get('letraMay');
				}else{
					$strClave = $strClave.', '. $request->get('letraMay');
				}
				if($regClave == ''){
					$regClave = $request->get('letraMayExp');
				}else{
					$regClave = $regClave.', '. $request->get('letraMayExp');
				}
			}			
			if(!is_null($numeroChk)){
				if($strClave == ''){
					$strClave = $request->get('numero');
				}else{
					$strClave = $strClave.', '. $request->get('numero');
				}
				if($regClave == ''){
					$regClave = $request->get('numeroExp');
				}else{
					$regClave = $regClave.', '. $request->get('numeroExp');
				}
			}			
			if(!is_null($caracterEspChk)){
				if($strClave == ''){
					$strClave = $request->get('caracterEsp');
				}else{
					$strClave = $strClave.', '. $request->get('caracterEsp');
				}
				if($regClave == ''){
					$regClave = $request->get('caracterEspExp');
				}else{
					$regClave = $regClave.', '. $request->get('caracterEspExp');
				}
			}			
			if(!is_null($blancoChk)){
				if($strClave == ''){
					$strClave = $request->get('blanco');
				}else{
					$strClave = $strClave.', '. $request->get('blanco');
				}
				if($regClave == ''){
					$regClave = $request->get('blancoExp');
				}else{
					$regClave = $regClave.', '. $request->get('blancoExp');
				}
			}
			
			$this->sessionSvc->setSession('strClave', $strClave);
			$this->sessionSvc->setSession('regClave', $regClave);

			if($strClave != ''){
				$empresa->setStrClave('minimo '.$request->get('longitud').' digitos, '.$strClave);
				$regClave = $request->get('longitud').','.$regClave;
				$empresa->setRegClave($regClave);
			}
			
			$em = $this->getDoctrine()->getManager(); 
			$em->persist($empresa);
			$em->flush();
		
/*
			$this->sessionSvc->setSession('letraMinChk', $letraMinChk);
			$this->sessionSvc->setSession('letraMayChk', $letraMayChk);
			$this->sessionSvc->setSession('numeroChk', $numeroChk);
			$this->sessionSvc->setSession('caracterEspChk', $caracterEspChk);
			$this->sessionSvc->setSession('blancoChk', $blancoChk);

*/
			return $this->render('CHOMPSGIBundle:Seguridad:claveNueva.html.twig', array('empresa' => $empresa)); 
		}else{
			return $this->redirect($this->generateUrl('chompsgi_homepage'));	
		}
	}	
	

    public function verAction($id){
		if ($this->sessionSvc->userIsLogged()){
			$rol = $this->getDoctrine()->getRepository('CHOMPSGIBundle:Rol')->find($id);
			if ($rol){
				$permisos =  $this->getDoctrine()->getRepository('CHOMPSGIBundle:Rol')-> listarPermisos($id);
				return $this->render('CHOMPSGIBundle:Seguridad:verRol.html.twig', array('rol' => $rol, 'permis' => $permisos));
			}else{
				throw $this->createNotFoundException('El Id no existe!');
			}
		}else{
			return $this->redirect($this->generateUrl('chompsgi_homepage'));	
		}
    }


    public function listarPermisosAction(){
		if ($this->sessionSvc->userIsLogged()){
			$allPermisos =  $this->getDoctrine()->getRepository('CHOMPSGIBundle:Rol')-> listarAllPermisos('cualqi');
			return $this->render('CHOMPSGIBundle:Seguridad:listarPermisos.html.twig', array('permis' => $allPermisos)); 
		}else{
			return $this->redirect($this->generateUrl('chompsgi_homepage'));	
		}
    }


    public function permisoNuevoAction(Request $request){
		if ($this->sessionSvc->userIsLogged()){
			$perm = new PermisoChomp();
	
			$perm->campo = 'ALL';
			$form = $this->createForm(new PermisoChompType(), $perm);
		
			$form->handleRequest($request);
			
			if ($form->get('save')->isClicked()) {
				if ($form->isValid()) {
					//To do: Validar Campos
					$em = $this->getDoctrine()->getManager();
					$perm->upsertPermiso($em);
							   
					return $this->redirect($this->generateUrl('permisos_listar'));   
				}
			}
	
			return $this->render('CHOMPSGIBundle:Seguridad:nuevo.html.twig', array(
				'form' => $form->createView(),
				));
		}else{
			return $this->redirect($this->generateUrl('chompsgi_homepage'));	
		}
    }


    public function nuevoRolAction (Request $req){
		if ($this->sessionSvc->userIsLogged()){
			$rolChomp = new RolChomp();
			$form = $this->createForm(new RolType(), $rolChomp);
			$form->handleRequest($req);
	
			if ($form ->isValid()){
				//guardar rol y permisos asociados
			}
			return $this->render('CHOMPSGIBundle:Seguridad:nuevoRol.html.twig', array(
						'form' => $form->createView(),
						'rolChomp' => $rolChomp));
		}else{
			return $this->redirect($this->generateUrl('chompsgi_homepage'));	
		}
    }
}

?>